<template>
  <div class="articleBox">
    <div class="name">第{{articleIndex+1}}章 {{articleList[articleIndex]?.name}}</div>
    <div class="content" v-html="articleList[articleIndex]?.content"></div>
  </div>
  <div class="toggleBox">
    <div class="toggleBtn" @click="toggle('top')">上一章</div>
    <div class="toggleBtn" @click="toggle('bottom')">下一章</div>
  </div>
</template>

<script setup>
  import {useRoute} from "vue-router"
  import {onBeforeMount,ref,reactive} from "vue"

  import {http} from "../config/axios/index.js"
  import {useUserStore} from "../config/pinia/user.js"

  //用户状态
  const userStore = useUserStore()

  //获取路由对象
  const route = useRoute()

  //文章列表
  const articleList = reactive([])

  //当前页面文章在数组中的下标
  let articleIndex = ref(0)

  //获取数据
  async function getData(){
    const res = await http({
      method: "GET",
      url: "article/get_article",
      params: {
        category: route.query.category
      }
    })
    if(res.data.code === 0){
      res.data.data.articleList.map((item,index)=>{
        articleList.push(item)
        if(item.bid === parseInt(route.query.id)){
          articleIndex.value = index
        }
      })
    }
  }

  //切换文章
  function toggle(type){
    switch (type) {
      case "top":
        articleIndex.value > 0 ? articleIndex.value-- : articleIndex.value = articleList.length - 1
        break
      case "bottom":
        articleIndex.value < articleList.length - 1 ? articleIndex.value++ : articleIndex.value = 0
    }
  }

  //组件挂载前
  onBeforeMount(()=>{
    getData()
  })

</script>

<style scoped>
  .articleBox{
    padding: 20px;
    width: 100%;
    height: calc(100vh - 59px);
    overflow: auto;
    background-color:  v-bind("userStore.bg");
    color: v-bind("userStore.color");
  }
  .articleBox .name{
    font-size: 30px;
    font-weight: bold;
    text-align: center;
  }
  .articleBox .content{
    padding-top: 20px;
  }
  .articleBox .content :deep(p){
    font-size: 18px;
    line-height: 1.5;
    text-indent: 2em;
    padding-bottom: 15px;
  }
  .toggleBox{
    position: fixed;
    right: 10px;
    top: 40%;
  }
  .toggleBox .toggleBtn{
    border: 2px solid #cccccc;
    display: block;
    text-decoration: none;
    writing-mode: vertical-rl;
    color: #ffffff;
    padding: 10px 5px;
    cursor: pointer;
    user-select: none;
    background: #F3904F;  /* fallback for old browsers */
    background: -webkit-linear-gradient(to bottom, #3B4371, #F3904F);  /* Chrome 10-25, Safari 5.1-6 */
    background: linear-gradient(to bottom, #3B4371, #F3904F); /* W3C, IE 10+/ Edge, Firefox 16+, Chrome 26+, Opera 12+, Safari 7+ */
  }
  .toggleBox .toggleBtn:last-child{
    margin-top: 20px;
  }
</style>